package com.opentool.web.controller;
import com.opentool.web.http.Response;
import com.opentool.web.module.LocalDnsRecord;
import com.opentool.web.service.ILocalDnsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
@RequestMapping("/")
@Controller
public class MainController {

    @Autowired
    private ILocalDnsService localDnsService;

    @RequestMapping("main")
    public ModelAndView index(ModelAndView modelAndView){
        modelAndView.setViewName("/home");
        return modelAndView;
    }

    @RequestMapping("setting")
    public ModelAndView setting(ModelAndView modelAndView){
        String dnsSvr=localDnsService.queryDnsServer();
        modelAndView.addObject("dnsSvr",dnsSvr);
        modelAndView.setViewName("/setting");
        return modelAndView;
    }

    @RequestMapping("domain")
    public ModelAndView domain(ModelAndView modelAndView){
        modelAndView.setViewName("/domain");
        return modelAndView;
    }

    @RequestMapping("/domainquery")
    @ResponseBody
    public Response getOnlines(HttpServletRequest request){
        try {
            List<LocalDnsRecord> rows= localDnsService.queryLocalDnsRecord();
            return new Response("0000","查询成功",rows);
        }catch (Exception e){
            return new Response("0001","查询失败");
        }
    }

    @RequestMapping("/ip")
    public ModelAndView local(HttpServletRequest request,ModelAndView modelAndView){
        String ip=this.getIpAddr(request);
        modelAndView.addObject("ip",ip);
        modelAndView.setViewName("/ipaddr");
        return modelAndView;
    }

    @RequestMapping("/domain/save")
    @ResponseBody
    public Response domainSave(LocalDnsRecord localDnsRecord, HttpServletRequest request){
        try {
            localDnsService.saveLocalDnsRecord(localDnsRecord);
            return new Response("0000","保存成功");
        }catch (Exception e){
            return new Response("0001","保存失败");
        }
    }



    @RequestMapping("/domain/delete/{id}")
    @ResponseBody
    public Response domainDelete(@PathVariable("id") Integer id, HttpServletRequest request){
        try {
            localDnsService.deleteLocalDnsRecord(id);
            return new Response("0000","删除成功");
        }catch (Exception e){
            return new Response("0001","删除失败");
        }
    }

    @RequestMapping("/dnsServer/save/{dnssvr}")
    @ResponseBody
    public Response dnsServerSave(@PathVariable("dnssvr") String dnssvr, HttpServletRequest request){
        try {
            localDnsService.saveDnsServer(dnssvr);
            return new Response("0000","保存成功");
        }catch (Exception e){
            return new Response("0001","保存失败");
        }
    }

    private String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        System.out.println("x-forwarded-for ip: " + ip);
        if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
            // 多次反向代理后会有多个ip值，第一个ip才是真实ip
            if( ip.indexOf(",")!=-1 ){
                ip = ip.split(",")[0];
            }
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            System.out.println("HTTP_X_FORWARDED_FOR ip: " + ip);
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Real-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}
